package com.zysdev.zysplayer

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.zysdev.zysplayer.databinding.ActivityMainBinding
import androidx.localbroadcastmanager.content.LocalBroadcastManager

import android.content.IntentFilter

import android.content.Intent

import android.content.BroadcastReceiver
import android.content.Context
import com.zysdev.zysplayer.service.PlayMusicService
import com.zysdev.zysplayer.utils.TimeUtil
import com.zysdev.zysplayer.R
import android.util.Log
import android.view.View
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import com.zysdev.zysplayer.player.ZYSPlayer
import com.zysdev.zysplayer.player.listener.OnPlayerListener


public class MainActivity : AppCompatActivity(), View.OnClickListener, OnPlayerListener {

    public lateinit var binding: ActivityMainBinding

    private var player: ZYSPlayer? = null

    //    private val mMusicDatas: List<MusicData> = ArrayList()
    private var totalTime = 0
    private var position = 0
    private var playState = false
    private var isFirstPlay = true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.ivPlayOrPause.setOnClickListener(this)
        binding.ivLast.setOnClickListener(this)
        binding.ivNext.setOnClickListener(this)
        binding.btnLeft.setOnClickListener(this)
        binding.btnRight.setOnClickListener(this)
        binding.btnCenter.setOnClickListener(this)
        binding.btnSpeed.setOnClickListener(this)
        binding.btnSpeedAdd.setOnClickListener(this)
        binding.btnPitch.setOnClickListener(this)
        binding.btnPitchAdd.setOnClickListener(this)
        binding.btnPitchSpeed.setOnClickListener(this)
        binding.musicSeekBar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                position = totalTime * progress / 100
                binding.tvCurrentTime.text = TimeUtil.duration2Time(progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {}
            override fun onStopTrackingTouch(seekBar: SeekBar) {
                seekTo(position)

            }
        })

        player = ZYSPlayer()
        player!!.setPlayerView(binding.playerView)
        player!!.setSource("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
        player!!.onPlayerListener = this
        player!!.prepare()
    }


    override fun onClick(v: View?) {
        if (v == binding.ivPlayOrPause) {
            if (isFirstPlay) {
                isFirstPlay = false
                playState = true
                binding.ivPlayOrPause.setImageResource(R.drawable.ic_pause)
                play()
                return
            }
            playState = !playState
            if (playState) {
                binding.ivPlayOrPause.setImageResource(R.drawable.ic_play)
                pause()
            } else {
                binding.ivPlayOrPause.setImageResource(R.drawable.ic_pause)
                resume()
            }
        } else if (v == binding.btnLeft) {
            left()
        } else if (v == binding.btnRight) {
            right()
        } else if (v == binding.btnCenter) {
            center()
        } else if (v == binding.ivNext) {
        } else if (v == binding.ivLast) {
        } else if (v == binding.btnSpeed) {
            //减速
            speed -= 0.1f
            if (speed <= 0) {
                speed = 0.1f
            }
            player?.setSpeed(speed)
        } else if (v == binding.btnSpeedAdd) {

            //减速
            speed += 0.1f
            if (speed > 2f) {
                speed = 2f
            }
            player?.setSpeed(speed)

        } else if (v == binding.btnPitch) {

            pitch -= 0.1f
            if (pitch <= 0f) {
                pitch = 0.1f
            }
            player?.setPitch(pitch)
        } else if (v == binding.btnPitchAdd) {
            pitch += 0.1f
            if (pitch > 2f) {
                pitch = 2f
            }
            player?.setPitch(pitch)
        } else if (v == binding.btnPitchSpeed) {
            speed = 1.0f
            pitch = 1.0f
            player?.setSpeed(speed)
            player?.setPitch(pitch)
        }
    }

    override fun onPrepared(total: Int) {
        this.totalTime = total
        binding.tvLoading.visibility = View.GONE
        binding.tvTotalTime.text = TimeUtil.secondsToDateFormat(totalTime, totalTime)

    }

    override fun onCurrentTime(current: Int, total: Int) {
        this.totalTime = total
        binding.musicSeekBar.progress = current * 100 / totalTime;
        binding.tvCurrentTime.text = TimeUtil.duration2Time(current)
        binding.tvTotalTime.text = TimeUtil.duration2Time(totalTime)
    }

    override fun onLoading(loading: Boolean) {
        if (loading) {
            binding.tvLoading.visibility = View.VISIBLE
        } else {
            binding.tvLoading.visibility = View.GONE
        }
    }


    var speed = 1.0f
    var pitch = 1.0f

    private fun play() {
        player?.play()
    }

    private fun pause() {
        player?.pause()
    }

    fun resume() {
        player?.resume()
    }

    fun left() {
        player?.setMute(1)
    }

    fun right() {
        player?.setMute(0)
    }

    fun center() {
        player?.setMute(2)
    }

    private fun stop() {
        player?.stop()
    }

    private fun seekTo(position: Int) {
        player?.seekTo(position)
    }

    private operator fun next() {

    }

    private fun last() {
    }

    override fun onDestroy() {
        super.onDestroy()
        stop()
    }


}